package com.cyf.plc.widget

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.cyf.plc.R
import kotlinx.android.synthetic.main.layout_sensor_view.view.*

/**
 * Author：Lei on 2020/12/10
 */
class SensorLinearLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : LinearLayout(context, attrs) {

    init {
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.SensorLinearLayout)
        val type: String? = typedArray.getString(R.styleable.SensorLinearLayout_sensorTvType)
        val data = typedArray.getString(R.styleable.SensorLinearLayout_sensorTvData)
        val unit: String? = typedArray.getString(R.styleable.SensorLinearLayout_sensorTvUnit)
        val darkFlag = typedArray.getBoolean(R.styleable.SensorLinearLayout_sensorDark,false)
        val view = LayoutInflater.from(context).inflate(
            R.layout.layout_sensor_view,
            this,
            true
        ) as LinearLayout
        view.gravity = Gravity.CENTER_VERTICAL
        var sourceHanSansCN = Typeface.createFromAsset(context.getAssets(), "fonts/BebasNenu.ttf")
        val tvDataType = view.findViewById<TextView>(R.id.tvDataType)
        val tvData = view.findViewById<TextView>(R.id.tvData)
        val tvDataUnit = view.findViewById<TextView>(R.id.tvDataUnit)
        tvData.typeface = sourceHanSansCN

        if (darkFlag) {
            view.setBackgroundResource(R.drawable.img_21_humidity_bg)
            tvDataType.setTextColor(Color.WHITE)
            tvData.setTextColor(Color.WHITE)
            tvDataUnit.setTextColor(Color.WHITE)
        } else {
            view.setBackgroundResource(R.drawable.img_sensor_bg)
        }
        if (!TextUtils.isEmpty(type)) {
            tvDataType.text = type
        }
        if (!TextUtils.isEmpty(data)) {
            tvData.text = data
        }
        if (!TextUtils.isEmpty(unit)) {
            tvDataUnit.text = unit
        }
    }

    fun sensorTvData(value : String?){
        value?.let {
            tvData.text = it
        }
    }
}